﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.RF;
using com.vs.Receiving.BL;
using com.vs.Inventory.BL;

namespace com.vs.RF.Web.receiving
{
    public partial class LPNEntry : RFDisplayPage
    {
        protected string stNofM = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            go();
        }

        protected override void parseRequest()
        {
            stErrorMessage = Request[RFSupport.ERROR_MESSAGE];
        }

        protected override void initData()
        {
            ReceiptContainerDO rcDO = null;
            List<ItemUnitOfMeasureDO> ltUOM = null;
            int iTotalQty = 0;
            int iTotalLPN = 0;
            int iLPNCount = 0;
            Queue<int> qLPNQty = null;

            try
            {
                if (Session[RFSupport.CONTAINER_COUNT] == null || Session[RFSupport.CONTAINER_NUMBER] == null)
                {
                    rcDO = (ReceiptContainerDO)Session[RFSupport.RECEIPT_CONTAINER_DO];
                    ltUOM = ItemManager.GetItemUOMIntoList(rfSess, rcDO.ITEM, rcDO.COMPANY, false);
                    iTotalQty = rcDO.QUANTITY;
                    qLPNQty = new Queue<int>();
                    foreach (ItemUnitOfMeasureDO umDO in ltUOM)
                    {
                        if (iTotalQty <= 0)
                            break;

                        if (iTotalQty / umDO.CONVERSION_QTY == 0)
                            continue;

                        if (StringManager.Equals(umDO.GROUP_DURING_CHECKIN, "Y"))
                        {
                            //如果在该数量级上合并收货，那么所有收货一个LPN就够了
                            iTotalLPN += 1;
                            qLPNQty.Enqueue(iTotalQty - iTotalQty % umDO.CONVERSION_QTY);
                            iTotalQty = iTotalQty % umDO.CONVERSION_QTY;
                        }
                        else
                        {
                            //如果不能合并收货，则每个单位需要一个LPN
                            iLPNCount = iTotalQty / umDO.CONVERSION_QTY;
                            iTotalLPN += iLPNCount;
                            iTotalQty = iTotalQty % umDO.CONVERSION_QTY;
                            for (int i = 0; i < iLPNCount; i++)
                                qLPNQty.Enqueue(umDO.CONVERSION_QTY);
                        }
                    }

                    //在Session中记录：LPN总数，当前LPN序号，每个LPN的数量（按从大到小的顺序）
                    Session[RFSupport.CONTAINER_COUNT] = iTotalLPN;
                    Session[RFSupport.CONTAINER_NUMBER] = 1;
                    Session[RFSupport.CONTAINER_QUANTITY_LIST] = qLPNQty;
                }

                stNofM = ((int)Session[RFSupport.CONTAINER_NUMBER]) + "/" + Session[RFSupport.CONTAINER_COUNT];
            }
            catch (Exception ex)
            {
                ExceptionManager.LogException(rfSess, ex);
                stErrorMessage = ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Server.Transfer("CheckInQtyEntry.aspx");
        }
    }
}
